<?php 
error_reporting(E_ALL);
include '../phpqrcode.php';
session_start();


require_once '../common/config.php';
require_once '../common/conn.php';
require_once '../common/function.php';
require_once '../db/account.db.php';
require_once '../db/point.db.php';



$action = $_POST['action'];



switch ($action) {
    case 'generateRechargeQrCodeUrl':
        $res = generateRechargeQrCodeUrl();
        break;
    case 'queryRecharge':
        $res = queryRecharge();
        break;
    default:
        $res['code'] = 2;
        $res['msg'] = '类型错误';
}

echo json_encode($res);

function generateRechargeQrCodeUrl(){

    $result = array();
    $payGame = $_POST['payGame'];
    $payGameVer = $_POST['payGameVer'];
    $payGameOpt = $_POST['payGameOpt'];
    $payAmount = $_POST['payAmount'];
    if($payAmount < 500){
        $result['code'] = 1;
        $result['msg'] = '赞助金额不能低于5元';
    }
    //$data = getJavaServerQrCodeUrl($_SESSION['AccountID'], $payGame, $payGameVer, $payGameOpt, $payAmount);
    $params = array(
        "payAccount" => $_SESSION['AccountID'],
        "payGame" => $payGame,
        "payGameVer" => $payGameVer,
        "payGameOpt" => $payGameOpt,
        "payAmount" => $payAmount
    );
    if(DEBUG){
        $res = doCurlPostRequest(API_RECHARGE_BASE_DEV . API_GENERATEQRURL, $params);
    }else{

        $res = doCurlPostRequest(API_RECHARGE_BASE . API_GENERATEQRURL, $params);
    }

    // $res = json_encode($res);
    // print_r($res);
    if($res['code'] == 200){
        $result['code'] = 0;
        // $result['res'] = $res;
        $result['msg'] = "成功";
        $result['amount'] = $payAmount;
        $result['outTradeNo'] = $res['data']['outTradeNo'];
        $result['qrCodeUrl'] = $res['data']['qrCodeUrl'];
        $result['imgUrl'] = generateQrUrlImage($res['data']['qrCodeUrl'], $_SESSION['AccountID'], $payAmount);
    }else{
        $result['code'] = 1;
        $result['msg'] = "支付宝支付二维码生成失败";
    }

    return $result;
}

function queryRecharge(){
    $result = array();
    $payGame = $_POST['payGame'];
    $payGameVer = $_POST['payGameVer'];
    $payGameOpt = $_POST['payGameOpt'];
    $outTradeNo = $_POST['outTradeNo'];

    $params = array(
        "payAccount" => $_SESSION['AccountID'],
        "payGame" => $payGame,
        "payGameVer" => $payGameVer,
        "payGameOpt" => $payGameOpt,
        "outTradeNo" => $outTradeNo
    );
    if(DEBUG){
        $res = doCurlPostRequest(API_RECHARGE_BASE_DEV . API_GETPAYSTATUS, $params);
    }else{
        $res = doCurlPostRequest(API_RECHARGE_BASE . API_GETPAYSTATUS, $params);
    }

    // $res = json_encode($res);
    // print_r($res);
    if($res['code'] == 200){
        $result['code'] = 0;
        // $result['res'] = $res;
        $result['payMsg'] = $res['data']['payMsg'];
        $result['payStatus'] = $res['data']['payStatus'];
        $result['subCode'] = $res['data']['subCode'];
        $result['msg'] = $res['data']['payMsg'];
    }else{
        $result['code'] = 1;
        $result['payMsg'] = $res['data']['payMsg'];
        $result['msg'] = $res['data']['subCode'];
    }

    return $result;
}
function generateQrUrlImage($url, $payAccount, $payAmount){
    // $curtime = getMillisecond();
    $curtime = time();
    // 二维码生成配置
    $errorCorrectionLevel = 'L';//容错级别   
    $matrixPointSize = 7;//生成图片大小
    //准备好的logo图片
    $logo = '../logo.jpg';
    // 存放路径
    $path = '../rechargeImg/';
    $QR = $path . trim($payAccount) . "_" . $payAmount . "_" . $curtime . '_qrcode_temp.png';//已经生成的原始二维码图
    $imageUrl = trim($payAccount) . "_" . $payAmount . "_" . $curtime . '_qrcode.png';
    $finalImage = $path . $imageUrl;
    
    

    QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 2);
    if ($logo !== FALSE) {   
        $QR = imagecreatefromstring(file_get_contents($QR));   
        $logo = imagecreatefromstring(file_get_contents($logo));   
        $QR_width = imagesx($QR);//二维码图片宽度   
        $QR_height = imagesy($QR);//二维码图片高度   
        $logo_width = imagesx($logo);//logo图片宽度   
        $logo_height = imagesy($logo);//logo图片高度   
        $logo_qr_width = $QR_width / 5;   
        $scale = $logo_width/$logo_qr_width;   
        $logo_qr_height = $logo_height/$scale;   
        $from_width = ($QR_width - $logo_qr_width) / 2;   
        //重新组合图片并调整大小   
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,   
        $logo_qr_height, $logo_width, $logo_height);   
    }   

    //输出图片   
    imagepng($QR, $finalImage);
    if(!DEBUG){
        $finalImage = './rechargeImg/' . $imageUrl;
    }
    return $finalImage;
}

function getMillisecond()
{
    list($s1, $s2) = explode(' ', microtime());
    return (float) sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
}

?>